import { reactive } from "vue";
export const useLoginRules = () => {
  // 用户名校验规则
  let validateName = async (_rule, value) => {
    if (value === "") {
      return Promise.reject("请输入用户名");
    } else {
      if (value.length < 2) {
        return Promise.reject("用户名不能小于2位");
      } else if (value.length > 20) {
        return Promise.reject("用户名不能大于20位");
      }
      return Promise.resolve();
    }
  };

  // 密码校验规则
  let validatePass = async (_rule, value) => {
    if (value === "") {
      return Promise.reject("请输入密码");
    } else {
      if (value.length < 6) {
        return Promise.reject("密码不能小于6位");
      } else if (value.length > 20) {
        return Promise.reject("密码不能大于20位");
      }
      return Promise.resolve();
    }
  };

  // 登录表单数据校验规则
  const loginRules = reactive({
    // 用户名
    username: [{ required: true, validator: validateName, trigger: "blur" }],
    // 密码
    password: [{ required: true, validator: validatePass, trigger: "blur" }],
  });

  return {
    loginRules,
  };
};
